package jplat.core.session;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import jplat.base.constant.KPlatResponseCode;
import jplat.base.constant.KTraceLog;
import jplat.error.exception.JSystemException;
import jplat.tools.string.JLogUtils;
import jplat.tools.string.XLog;

import org.apache.log4j.Logger;

import ebcs.scan.bean.sys.SysUserInfor;

/**
 * 会话实现类.
 * @author zhangcq
 *
 */
public class JHttpSession implements JSession
{
	HttpSession session;
	HttpServletRequest request;
	
	private static Logger logger = Logger.getLogger(JHttpSession.class);
	
	public JHttpSession( HttpServletRequest req, boolean create ) throws JSystemException
	{
		request = req;
		
		//强制作废会话.
		if( create && request.isRequestedSessionIdValid() )
		{
			HttpSession sess = req.getSession();
			logger.info(JLogUtils.getTraceLog(KTraceLog.ACTION_SESSION, KTraceLog.EVENT_POINT,"",JLogUtils.buildUserData("force-destroy",sess.getId())));
			try
			{
				if (sess != null) sess.invalidate();
			}
			catch ( Exception e )
			{
				logger.error(JLogUtils.getTraceLog(KTraceLog.ACTION_SESSION, KTraceLog.EVENT_FAIL, "",
													JLogUtils.buildUserData("sess.invalidate",e.getMessage(),sess.getId())));
			}
		}

		session = req.getSession(create);
		if ( session != null )
		{
			XLog.log("__CREATE_SESSION:ID="+session.getId()+",isNEW="+(session.isNew()?"true":"false"));
		}
	
		
		//不创建,但是没找到则超时.
		if ( !create && session == null )
		{
			throw new JSystemException(KPlatResponseCode.CD_NO_SESSION,KPlatResponseCode.MSG_NO_SESSION);
		}
	}
	
	@Override
	public String getSessionID()
	{
		if ( session != null )
		{
			return session.getId();
		}
		
		return null;
	}
	
	@Override
	public boolean isValid()
	{
		if ( session == null /*|| request.getSession( false ) == null */)
		{
			return false;
		}
		
		return true;
	}
	
	@Override
	public String getValue(String key) {
		// TODO Auto-generated method stub
		return (String)getObj(key);
	}

	@Override
	public boolean setValue(String key, String value) {
		// TODO Auto-generated method stub
		return setObj(key,value);
	}
	
	@Override
	public Object getObj(String key) {
		// TODO Auto-generated method stub
		
		if ( session != null )
		{
			return session.getAttribute(key);
		}
		
		return null;
	}
	
	@Override
	public boolean setObj(String key, Object value) {
		// TODO Auto-generated method stub
		if ( session != null )
		{
			session.setAttribute(key, value);
			return true;
		}
		
		return false;
	}
	
	@Override
	public boolean removeObj(String key) {
		// TODO Auto-generated method stub
		if ( session != null )
		{
			session.removeAttribute(key);
			return true;
		};
		
		return false;
	}
	
	@Override
	public String getEncKey(String type) {
		// TODO Auto-generated method stub
		return (String)getObj(K_ENC_KEY);
	}

	@Override
	public boolean setEncKey( String type, String encKey ) {
		// TODO Auto-generated method stub
		return setObj(K_ENC_KEY+type,encKey);
	}
	
	@Override
	public boolean setUserInfo(Object obj) {
		// TODO Auto-generated method stub
		return setObj(K_USER_INFO,obj);
	}

	@Override
	public Object getUserInfo() {
		// TODO Auto-generated method stub
		return getObj(K_USER_INFO);
	}

	@Override
	public int sessType() {
		// TODO Auto-generated method stub
		return JSession.SESS_HTTP;
	}

	@Override
	public boolean isNew() {
		// TODO Auto-generated method stub
		return session.isNew();
	}
	
	@Override
	public boolean removeValue(String key) {
		// TODO Auto-generated method stub
		return removeObj(key);
	}

	/******
	 * 		 CTHttpSession特有的getter-setters
	 ******/
	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public boolean destroy() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List<SysUserInfor> getAllLoginUserInfor() {
		// TODO Auto-generated method stub
		return null;
	}

}
